package com.amt.olcacon.utils;

import org.openlca.core.matrix.index.EnviFlow;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.model.descriptors.ImpactDescriptor;
import org.openlca.core.results.EnviFlowValue;
import org.openlca.core.results.ImpactValue;
import org.openlca.core.results.LcaResult;
import org.openlca.core.results.TechFlowValue;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 工具类：封装 LcaResult 的常用操作
 */
public class LcaResultHelper {

    private final LcaResult result;

    public LcaResultHelper(LcaResult result) {
        this.result = result;
    }


    /**
     * 获取某个指标下，各过程的贡献情况
     *
     * @param indicator 指标描述
     * @return map: Process -> (总值, 直接值, 占比)
     */
    public Map<String, Double> getPerProcessImpact(ImpactDescriptor indicator) {
        Map<String, Double> p = new LinkedHashMap<>();

        // 整个系统该指标的总值
        double systemTotal = result.getTotalImpactValueOf(indicator);

        // 获取指标 × 过程贡献矩阵
        List<ImpactValue> totalImpactsMatrix = result.getTotalImpacts();

        // 找到这个 indicator 的矩阵列索引
        int impactIdx = result.impactIndex().of(indicator);
        if (impactIdx < 0) {
            return null;
        }

        // 遍历所有过程
        result.techIndex().each((procIdx, process) -> {
            double procTotal = totalImpactsMatrix.get(procIdx).value();
        });


        return p;
    }

    /**
     * 获取所有环境流的总量
     */
    public Map<String, Double> getPerProcessFlow(FlowDescriptor flow) {
        Map<String, Double> f = new HashMap<>();
        double systemTotalFlow = result.getTotalFlowValueOf(EnviFlow.inputOf(flow));
        List<EnviFlowValue> totalFlowsMatrix = result.getTotalFlows();
        List<TechFlowValue> directFlowsMatrix = result.getDirectFlowValuesOf(EnviFlow.inputOf(flow));

        int flowIdx = result.enviIndex().of(flow);
        if (flowIdx < 0) {
            return null;
        }

        result.techIndex().each((procIdx, process) -> {
            double total = totalFlowsMatrix.get(procIdx).value();
            double direct = directFlowsMatrix.get(procIdx).value();
            double share = (systemTotalFlow != 0 ? total / systemTotalFlow : 0.0);
        });
        return f;
    }

    /**
     * 获取所有环境流的总量
     */
    public Map<String, Double> getTotalFlows() {
        Map<String, Double> flows = new HashMap<>();
        result.enviIndex().each((i, flow) -> {
            double value = result.getTotalFlowValueOf(flow);
            //flow.flow().
            flows.put(flow.flow().name, value);
        });
        return flows;
    }

    /**
     * 获取所有影响指标的总量
     */
    public Map<String, Double> getTotalImpacts() {
        Map<String, Double> impacts = new HashMap<>();
        result.impactIndex().each((i, indicator) -> {
            double value = result.getTotalImpactValueOf(indicator);
            impacts.put(indicator.name, value);
        });
        return impacts;
    }

}